package com.escape.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.escape.subject.application.convert.SubjectCategoryDTOConvert;
import com.escape.subject.application.dto.SubjectCategoryDTO;
import com.escape.subject.common.entity.Result;
import com.escape.subject.domain.entity.SubjectCategoryBO;
import com.escape.subject.domain.service.SubjectCategoryDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: escape_zebra
 * @CreateTime: 2024-12-18  12:30
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/subject/category")
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            // if 判断作用：在高并发情况下会先将后面 json 序列化先执行一遍(日志优化)
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.DTO:{}", JSON.toJSONString(subjectCategoryDTO));
            }

            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDTO.getCategoryName()),"分类名不能为空!");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"父节点不能为空!");

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE.convertDtoToBO(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error: {}", e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询岗位分类大类
     * @return
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert .INSTANCE.convertDtoToBO(subjectCategoryDTO);
            List<SubjectCategoryBO> boList = subjectCategoryDomainService.queryCategoryBO(subjectCategoryBO);
            List<SubjectCategoryDTO> dtoList = SubjectCategoryDTOConvert.INSTANCE.convertBoListToDtoList(boList);
            return Result.ok(dtoList);
        }catch (Exception e){
            log.error("SubjectCategoryController.queryPrimaryCategory.error: {}", e.getMessage(), e);
            return Result.fail("查询失败!");
        }
    }

    /**
     * 查询大类下的子类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId());
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE.convertDtoToBO(subjectCategoryDTO);
            List<SubjectCategoryBO> boList = subjectCategoryDomainService.queryCategoryBO(subjectCategoryBO);
            List<SubjectCategoryDTO> dtoList = SubjectCategoryDTOConvert.INSTANCE.convertBoListToDtoList(boList);
            return Result.ok(dtoList);
        }catch (Exception e){
            log.error("SubjectCategoryController.queryCategoryByPrimary.error: {}", e.getMessage(), e);
            return Result.fail("查询失败!");
        }
    }

    /**
     * 更新分类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            if(log.isInfoEnabled()) {
                log.info("SubjectCategoryController.update.DTO:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE.convertDtoToBO(subjectCategoryDTO);
            Boolean res = subjectCategoryDomainService.update(subjectCategoryBO);
            return Result.ok(res);
        }catch (Exception e){
            log.error("SubjectCategoryController.update error: {}", e.getMessage(), e);
            return Result.fail("更新分类失败!");
        }
    }

    /**
     * 删除分类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.delete.DTO:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE.convertDtoToBO(subjectCategoryDTO);
            Boolean res = subjectCategoryDomainService.delete(subjectCategoryBO);
            return Result.ok(res);
        }catch (Exception e){
            log.error("SubjectCategoryController.delete.error: {}", e.getMessage(), e);
            return Result.fail("删除分类失败!");
        }
    }

}
